﻿#region Copyrigth and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ezsocio
 * ==============================================================================*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace CNdotnet.Ezsocio.ContactService.Email {
    static class WangyiResover {
        #region Fields

        #endregion

        #region Constructors

        #endregion

        #region Properties

        #endregion

        #region Events

        #endregion

        #region Methos
        public static MailContractCollection Resove(string html) {
            MailContractCollection mc = new MailContractCollection();
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(html);
            var root = doc.DocumentElement;
            string code = root.SelectSingleNode("code").InnerText;
            if (code == "S_OK") {
                var objectNode = root.SelectSingleNode("array/object/array");
                var objectNodes = objectNode.SelectNodes("object");
                foreach (XmlNode n in objectNodes) {
                    MailContract m = new MailContract();
                    XmlNode n1 = n.SelectSingleNode("string[@name='EMAIL;PREF']");
                    if (n1 != null) {
                        m.Email = n1.InnerText;
                        m.Name = m.Email;
                        XmlNode n2 = n.SelectSingleNode("string[@name='FN']");
                        if (n2 != null) {
                            string name = n2.InnerText;
                            if (!string.IsNullOrEmpty(name)) {
                                m.Name = name;
                                if (name.StartsWith("/mg")) {
                                    m.Name = name.Substring(3);
                                }
                            }
                        }                       
                        mc.Add(m);
                    }
                }
            }
            return mc;
        }
        #endregion
    }
}
